#pragma once

/*
 UTF16:
 00000000 - 0000FFFF	 xxxxxxxx-xxxxxxxx
 00010000 - 0010FFFF	 110110yy-yyxxxxxx 110111xx-xxxxxxxx

 UTF8:
 00000000 - 0000007F	    0xxxxxxxx
 00000080 - 000007FF	    110xxxxx 10xxxxxx
 00000800 - 0000FFFF	    1110xxxx 10xxxxxx 10xxxxxx
 00010000 - 001FFFFF	    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
 00200000 - 03FFFFFF*	111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
 04000000 - 7FFFFFFF*	1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx


 */

const int UTF8MaxBytes = 4;

unsigned int UTF8Length(const wchar_t* uptr, unsigned int tlen);
void UTF8FromUTF16(const wchar_t* uptr, unsigned int tlen, char* putf, unsigned int len);
unsigned int UTF8CharLength(unsigned char ch);
unsigned int UTF16Length(const char* s, unsigned int len);
unsigned int UTF16FromUTF8(const char* s, unsigned int len, wchar_t* tbuf, unsigned int tlen);

extern int UTF8BytesOfLead[256];
void UTF8BytesOfLeadInitialise();

inline bool UTF8IsTrailByte(int ch) {
	return (ch >= 0x80) && (ch < 0xc0); //[10000000, 11000000)
}

inline bool UTF8IsAscii(int ch) {
	return ch < 0x80;
}

enum {
	UTF8MaskWidth = 0x7, UTF8MaskInvalid = 0x8
};
int UTF8Classify(const unsigned char* us, int len);

#define WIDE_TEXT_LEN	512

class TextWide {
	wchar_t fixbuf[WIDE_TEXT_LEN];
public:
	wchar_t* buf;
	int len;
public:
	TextWide(const char* s, int len_);
	virtual ~TextWide();
};
